﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using Worktool.CODE_Chris.DataSourceHelpers;
using Worktool.CODE_Chris.SQL_DATA;

namespace Worktool
{
    public partial class MaterialDetail : Form
    {
        private SQL sql;
        private Boolean isInit = true;
        private MaterialDetailHelper helper;

        public MaterialDetail()
        {
            InitializeComponent();
            this.Text = "Material Details: " + Login.currentDBName;
            init();
        }

        private void init()
        {
            sql = new SQL();
            helper = new MaterialDetailHelper(dataGridView1, this);

            fillTablesInit();

            isInit = false;
        }

        #region Init
        private void fillTablesInit()
        {
            try
            {
                helper.getData1();
            }
            catch (Exception e)
            {

            }
        }

        private void MaterialDetail_Load(object sender, EventArgs e)
        {
            typeof(DataGridView).InvokeMember("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty, null, dataGridView1, new object[] { true });
        }
        #endregion

        #region Filter
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text == "" && textBox2.Text == "")
            {
                if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = null;
            }
            else if (textBox1.Text == "" && textBox2.Text != "")
            {
                if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("Manufacturer LIKE '{0}'", (textBox2.Text + "*"));
            }
            else if (textBox1.Text != "" && textBox2.Text == "")
            {
                if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("Venue LIKE '{0}'", (textBox1.Text + "*"));
            }
            else if (textBox1.Text != "" && textBox2.Text != "")
            {
                if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("Venue LIKE '{0}' AND Manufacturer LIKE '{1}'", (textBox1.Text + "*"), (textBox2.Text + "*"));
            }
        }
        #endregion

        #region Export
        private void button4_Click(object sender, EventArgs e)
        {
            dataGridView1.SelectAll();
            DataObject dataObj = dataGridView1.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
            Microsoft.Office.Interop.Excel.Application xlexcel;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            xlexcel = new Microsoft.Office.Interop.Excel.Application();
            xlexcel.Visible = true;
            xlWorkBook = xlexcel.Workbooks.Add(misValue);
            xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
            CR.Select();
            xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
            dataGridView1.ClearSelection();
        }
        #endregion

        #region Daten neu laden
        public void reloadAllData()
        {
            Cursor.Current = Cursors.WaitCursor;
            this.Enabled = false;
            fillTablesInit();
            this.Enabled = true;
            Cursor.Current = Cursors.Default;
        }
        #endregion
    }
}
